﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace Ocmf.Types
{
    public enum ChamberState
    {
        Error,
        Idle,
        Running
    }
    
    public class Chamber
    {
        public long ChamberId { get; private set; }
        public string HostName { get; set; }
        public string IpAddress { get; set; }
        public ChamberState LastKnownStatus { get; private set; }
        public DateTime LastSeenAlive { get; private set; }
        public Experiment RunningExperiment { get; set; }

        public Chamber(long chamberId, string hostName, string ipAddress, ChamberState lastKnownStatus, DateTime lastSeenAlive, Experiment runningExperiment)
        {
            ChamberId = chamberId;
            HostName = hostName;
            IpAddress = ipAddress;
            LastKnownStatus = lastKnownStatus;
            LastSeenAlive = lastSeenAlive;
            RunningExperiment = runningExperiment;
        }

        public static void FromXml(string xmlFilePath, Chamber[] chambers)
        {
            //create xml document and file declaration
            var xmlDocument = new XmlDocument();
            var xmlDeclaration = xmlDocument.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            xmlDocument.AppendChild(xmlDeclaration);

            var chambersElement = xmlDocument.CreateElement("Chambers");

            foreach(var chamber in chambers)
            {
                var chamberElement = xmlDocument.CreateElement("Chamber");

                var chamberIdAttribute = xmlDocument.CreateAttribute("ChamberId");
                chamberIdAttribute.InnerText = chamber.ChamberId.ToString();
                chamberElement.Attributes.Append(chamberIdAttribute);

                var hostNameAttribute = xmlDocument.CreateAttribute("HostName");
                hostNameAttribute.InnerText = chamber.HostName;
                chamberElement.Attributes.Append(hostNameAttribute);

                var ipAddressAttribute = xmlDocument.CreateAttribute("IPAddress");
                ipAddressAttribute.InnerText = chamber.IpAddress;
                chamberElement.Attributes.Append(ipAddressAttribute);

                var lastKnownStatusAttribute = xmlDocument.CreateAttribute("LastKnownStatus");
                lastKnownStatusAttribute.InnerText = Enum.GetName(typeof(ChamberState), chamber.LastKnownStatus);
                chamberElement.Attributes.Append(lastKnownStatusAttribute);

                var lastSeenAlive = xmlDocument.CreateAttribute("LastSeenAlive");
                lastSeenAlive.InnerText = chamber.LastSeenAlive.ToString();
                chamberElement.Attributes.Append(lastSeenAlive);

                chambersElement.AppendChild(chamberElement);
            }

            xmlDocument.AppendChild(chambersElement);

            xmlDocument.Save(xmlFilePath);
        }

        //public static Chamber[] FromXml(string xmlFilePath)
        //{
        //    var chamberList = new List<Chamber>();

        //    var xmlDocument = new XmlDocument();
        //    xmlDocument.Load(xmlFilePath);

        //    foreach(XmlElement chamberXml in xmlDocument["Chambers"].ChildNodes)
        //    {
        //        var newChamber = new Chamber(
        //            long.Parse(chamberXml.Attributes["ChamberId"].InnerText),
        //            chamberXml.Attributes["HostName"].InnerText,
        //            chamberXml.Attributes["IPAddress"].InnerText,
        //            (ChamberState) Enum.Parse(typeof (ChamberState), chamberXml.Attributes["LastKnownStatus"].InnerText),
        //            DateTime.Parse(chamberXml.Attributes["LastSeenAlive"].InnerText));
        //        chamberList.Add(newChamber);
        //    }

        //    return chamberList.ToArray();
        //}
    }
}
